package com.space.service.impl;

import com.space.dao.CityDao;
import com.space.dao.ExpenseDao;
import com.space.dao.FlightDao;
import com.space.entity.*;
import com.space.exception.MyException;
import com.space.message.MyMessage;
import com.space.service.FlightService;
import com.space.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author : 港某
 * @date : 2020/2/13 16:51
 */
@Service
public class FlightServiceImpl implements FlightService {

    @Autowired
    FlightDao flightDao;
    @Autowired
    CityDao cityDao;
    @Autowired
    ExpenseDao expenseDao;

    @Override
    public List<Flight> selectAllFlight() {
        return flightDao.selectAllFlight();
    }

    @Override
    public List<Integer> selectSomeFlightByHot() {
        return flightDao.selectSomeFlightByHot();
    }

    @Override
    public List<Flight> selectSomeFlightByExpense(int expenseId, int userId) {
        Map<String, Integer> tmp = new HashMap<>(3);
        tmp.put("userId", userId);
        tmp.put("expenseId", expenseId);
        Expense expense = expenseDao.selectExpenseById(tmp);
        if (expense == null) {
            return null;
        }
        Flight flight = flightDao.selectFlightById(expense.getExpenseFlight().getFlightId());
        if (flight == null) {
            return null;
        }
        Map<String, String> map = new HashMap<>(5);
        map.put("flightStartAreaId", flight.getFlightStartArea().getCityId() + "");
        map.put("flightEndAreaId", flight.getFlightEndArea().getCityId() + "");
        map.put("flightId", expense.getExpenseFlight().getFlightId() + "");
        return flightDao.selectSomeFlightByExpense(map);
    }

    @Override
    public List<Flight> selectFlightByUserSearch(String flightStartArea, String flightEndArea, String flightStartTime, boolean check) {
        if (flightStartArea != null && flightEndArea != null) {
            City startCity = cityDao.selectCityByName(flightStartArea);
            City endCity = cityDao.selectCityByName(flightEndArea);
            if (startCity != null && endCity != null) {
                Map<String, String> map = new HashMap<>(5);
                map.put("flightStartAreaId", startCity.getCityId() + "");
                map.put("flightEndAreaId", endCity.getCityId() + "");
                if (flightStartTime != null) {
                    if (MyUtil.checkTime(flightStartTime)) {
                        flightStartTime += " 00:00:00";
                        map.put("flightStartTime", flightStartTime);
                        map.put("flightEndTime", MyUtil.longDateToString(MyUtil.stringDateToLong(flightStartTime) + 86400000));
                        if (check) {
                            map.put("check", "check");
                        }
                        return flightDao.selectSomeFlightByUserSearch(map);
                    }
                }
            }
        }
        return null;
    }

    @Override
    public List<Map<String, String>> selectFlightByAi(String flightStartArea, String flightEndArea, String flightStartTime) {
        List<Map<String, String>> mapList = new LinkedList<>();
        if (flightStartArea != null && flightEndArea != null && flightStartTime != null) {
            if (!MyUtil.checkTime(flightStartTime)) {
                return null;
            }
            City startCity = cityDao.selectCityByName(flightStartArea);
            City endCity = cityDao.selectCityByName(flightEndArea);
            if (startCity != null && endCity != null) {
                List<Flight> flightListByStartCityId = flightDao.selectFlightByStartCityId(startCity.getCityId());
                if (flightListByStartCityId == null || flightListByStartCityId.size() == 0) {
                    return null;
                }
                List<Flight> flightListByEndCityId = flightDao.selectFlightByEndCityId(endCity.getCityId());
                if (flightListByEndCityId == null || flightListByEndCityId.size() == 0) {
                    return null;
                }
                for (Flight flightStart : flightListByStartCityId) {
                    for (Flight flightEnd : flightListByEndCityId) {
                        if (flightStart.getFlightEndArea().getCityId() == flightEnd.getFlightStartArea().getCityId()) {
                            if (MyUtil.stringDateToLong(flightStart.getFlightEndTime()) < MyUtil.stringDateToLong(flightEnd.getFlightStartTime())) {
                                Map<String, String> map = new HashMap<>(10);
                                map.put("flightStartId", flightStart.getFlightId() + "");
                                map.put("flightEndId", flightEnd.getFlightId() + "");
                                map.put("flightStartArea", flightStart.getFlightStartArea().getCityName());
                                map.put("flightChangeArea", flightStart.getFlightEndArea().getCityName());
                                map.put("flightEndArea", flightEnd.getFlightEndArea().getCityName());
                                map.put("flightStartTime", flightStart.getFlightStartTime());
                                map.put("flightEndTime", flightEnd.getFlightEndTime());
                                mapList.add(map);
                            }
                        }
                    }
                }
                return mapList;
            }
        }
        return null;
    }

    @Override
    public Map<String, List<Flight>> selectHotFlight() {
        Map<String, List<Flight>> map = new HashMap<>(10);
        List<Integer> hotCityIdList = flightDao.selectSomeFlightByHot();
        for (Integer integer : hotCityIdList) {
            int id = integer;
            List<Flight> flightList = flightDao.selectFlightByStartCityId(id);
            map.put(id + "", flightList);
        }
        return map;
    }

    @Override
    public int selectAllFlightCount() {
        return flightDao.selectAllFlightCount();
    }

    @Override
    public void bookFlight(String flightId1, String flightId2, User user) {
        Map<String, Integer> map = new HashMap<>(5);
        if (flightId1 != null) {
            if (MyUtil.checkNumber(flightId1)) {
                Flight flight = flightDao.selectFlightById(Integer.parseInt(flightId1));
                map.put("userId", user.getUserId());
                map.put("flightId", flight.getFlightId());
                expenseDao.insertExpense(map);
            }
        }
        if (flightId2 != null && flightId1 != null) {
            if (MyUtil.checkNumber(flightId2) && MyUtil.checkNumber(flightId1)) {
                Flight flight = flightDao.selectFlightById(Integer.parseInt(flightId2));
                map.put("userId", user.getUserId());
                map.put("flightId", flight.getFlightId());
                expenseDao.insertExpense(map);
            }
        }
    }

    @Override
    public void insertFlight(String flightStartArea, String flightEndArea, String flightStartTime, String flightPrice) throws MyException {
        String flightEndAreaIdString = cityDao.selectCityIdByName(flightEndArea);
        String flightStartAreaIdString = cityDao.selectCityIdByName(flightStartArea);
        int flightEndAreaId = 1;
        int flightStartAreaId = 1;
        if (flightStartAreaIdString != null && flightEndAreaIdString != null) {
            flightEndAreaId = Integer.parseInt(flightEndAreaIdString);
            flightStartAreaId = Integer.parseInt(flightStartAreaIdString);
        }
        if (flightEndAreaId == flightStartAreaId) {
            throw new MyException(MyMessage.FLIGHT_AREA_ERROR);
        }
        if (flightStartTime == null || "".equals(flightStartTime)) {
            throw new MyException(MyMessage.FLIGHT_TIME_ERROR);
        }
        City startCity = cityDao.selectCityById(flightStartAreaId);
        City endCity = cityDao.selectCityById(flightEndAreaId);
        Map<String, String> map = new HashMap<>(5);
        map.put("flightStartAreaId", flightStartAreaId + "");
        map.put("flightEndAreaId", flightEndAreaId + "");
        map.put("flightStartTime", flightStartTime);
        map.put("flightEndTime", MyUtil.getEndTime(startCity, endCity, flightStartTime));
        map.put("flightPrice", flightPrice);
        flightDao.insertFlight(map);
    }

    @Override
    public void deleteFlightById(int[] flightIdArr) {
        for (int value : flightIdArr) {
            Flight flight = flightDao.selectFlightById(value);
            if (flight.getFlightStatus().getStatusTypeId() == 2) {
                continue;
            }
            flightDao.deleteFlightById(value);
        }
    }

    @Override
    public Rflight selectSomeFlightByPage(String nowPage, String flightStartTime, String flightStartArea, String flightEndArea) {
        Map<String, String> map = new HashMap<>(10);
        // 航班管理分页显示条数：8
        int pageSize = 8;
        int flightCount;
        if (flightStartTime != null && !"".equals(flightStartTime)) {
            flightStartTime += " 00:00:00";
            long l1 = MyUtil.stringDateToLong(flightStartTime);
            String flightEndTime = MyUtil.longDateToString(l1 + 86400000);
            map.put("flightStartTime", flightStartTime);
            map.put("flightEndTime", flightEndTime);
        }
        if (flightStartArea != null && !"".equals(flightStartArea)) {
            City startCity = cityDao.selectCityByName(flightStartArea);
            if (startCity != null) {
                map.put("flightStartAreaId", startCity.getCityId() + "");
            }
        }
        if (flightEndArea != null && !"".equals(flightEndArea)) {
            City endCity = cityDao.selectCityByName(flightEndArea);
            if (endCity != null) {
                map.put("flightEndAreaId", endCity.getCityId() + "");
            }
        }
        flightCount = flightDao.selectAllFlightBySearch(map).size();
        int pageCount = flightCount % pageSize == 0 ? flightCount / pageSize : flightCount / pageSize + 1;
        if (nowPage == null) {
            nowPage = "1";
        }
        if (Integer.parseInt(nowPage) == -1) {
            nowPage = pageCount + "";
        }
        map.put("startPage", pageSize * (Integer.parseInt(nowPage) - 1) + 1 + "");
        map.put("endPage", pageSize * Integer.parseInt(nowPage) + "");
        return new Rflight(flightDao.selectSomeFlightByPage(map), pageCount);
    }

    @Override
    public Flight selectFlightById(int flightId) {
        return flightDao.selectFlightById(flightId);
    }

    @Override
    public void updateFlightById(int flightId, String flightStartTime, String flightPrice) {
        Flight flight = flightDao.selectFlightById(flightId);
        City startCity = cityDao.selectCityById(flight.getFlightStartArea().getCityId());
        City endCity = cityDao.selectCityById(flight.getFlightEndArea().getCityId());
        Map<String, String> map = new HashMap<>(5);
        map.put("flightStartTime", flightStartTime);
        map.put("flightEndTime", MyUtil.getEndTime(startCity, endCity, flightStartTime));
        map.put("flightId", flightId + "");
        map.put("flightPrice", flightPrice);
        flightDao.updateFlightById(map);
    }

    @Override
    public void updateFlightStartStatusId() {
        flightDao.updateFlightStartStatusId();
    }

    @Override
    public void updateFlightEndStatusId() {
        flightDao.updateFlightEndStatusId();
    }

    @Override
    public FlightStatusCount selectSomeFlightCount() {
        int flightStatusCount = flightDao.selectAllFlightCount();
        int flightStatusCount1 = flightDao.selectFlightCountByStatus1();
        int flightStatusCount2 = flightDao.selectFlightCountByStatus2();
        int flightStatusCount3 = flightDao.selectFlightCountByStatus3();
        return new FlightStatusCount(flightStatusCount, flightStatusCount1, flightStatusCount2, flightStatusCount3);
    }


}
